package com.qipay.sp.excutor.tangchao;

import com.alibaba.fastjson.JSONObject;
import com.qipay.baas.model.Order;
import com.qipay.order.config.OrderStateEnum;
import com.qipay.order.service.OrderService;
import com.qipay.scalar.type.ApiStatusEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

@Component
public class TangchaoNativeExcutor extends AbstractExcutor {

    private final static String API_URL = "api_url";
    @Autowired
    OrderService orderService;
    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put("price",
                new BigDecimal(order.getAmount()).divide(new BigDecimal("100"),2,BigDecimal.ROUND_DOWN));
        apiParams.put("out_trade_no",order.getTradeNo());
        apiParams.put("pay_user_id",orderService.createOrder());
        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) {
        Object apiUrl = getVal(spconfig, API_URL);
        Object id = getVal(spconfig, "id");//商户uid
        Object notifyUrl= getVal(spconfig, "notify_url");
        Object returnUrl= getVal(spconfig, "return_url");
        Object key= getVal(spconfig, "key");

        params.put("id",id);
        params.put("notify_url",notifyUrl);
        params.put("return_url",returnUrl);

        String urlStr = getParamStr1(params);
        String signStr = getParamStr(params);

        String sign = MD5Util.sign(signStr+key.toString(),"UTF-8").toLowerCase();
        params.put("sign",sign);
        urlStr = urlStr+"&sign="+sign;

        String url = apiUrl.toString()+"?format=html&"+urlStr;

        Order order = orderService.getByTradeNo(params.get("out_trade_no").toString());
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("status", ApiStatusEnum.OK.getCode());
        resultMap.put("pay_info", url);
        order.setState(OrderStateEnum.WAITING.getType());

        orderService.update(order);
        return resultMap;

    }
    public String getParamStr1(Map<String, Object> paramMap) {
        SortedMap<String, Object> smap = new TreeMap<String, Object>(paramMap);
        StringBuffer stringBuffer = new StringBuffer();
        for (Map.Entry<String, Object> m : smap.entrySet()) {
            Object value = m.getValue();
            if (value != null && StringUtils.isNotBlank(String.valueOf(value))) {
                try {
                    stringBuffer.append(m.getKey()).append("=").append(URLEncoder.encode(value.toString(),"UTF-8")).append("&");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());
        return stringBuffer.toString();
    }
}
